//
//  WLComposeTextView.swift
//  WLWeibo
//
//  Created by Devil on 16/8/17.
//  Copyright © 2016年 Wiliam. All rights reserved.
//

import UIKit

class WLComposeTextView: UITextView {
    //重写textView font属性
    override var font: UIFont?{
    
        didSet{
            //设置占位文字的font
            placeholderLabel.font = font
        }
    }
    
    var placeholder: String?{
    
        didSet{
            //设置占位文字的text
            placeholderLabel.text = placeholder
        }
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置视图
    private func setupUI(){
    
        backgroundColor = RandomColor()
        //添加控件
        addSubview(placeholderLabel)
        placeholderLabel.snp_makeConstraints { (make) in
            make.top.equalTo(8)
            make.leading.equalTo(5)
            make.width.equalTo(SCREENW - 10)
        }
        //监听系统通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textViewTextDidChange", name: UITextViewTextDidChangeNotification, object: nil)
    
    }
    //监听文字变化
    func textViewTextDidChange(){
        placeholderLabel.hidden = self.hasText()
    }
    //懒加载控件
    private lazy var placeholderLabel: UILabel = {
    
        let lab = UILabel()
        lab.textColor = UIColor.grayColor()
        lab.numberOfLines = 0
        return lab
    }()
    //析构函数
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    

}
